package com.bachelor.community.mapper;

import com.bachelor.community.model.entity.Answer;
import com.bachelor.community.model.vo.AnswerVO;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface AnswerMapper extends BaseMapper<Answer> {
    @Select("select answer.*, " +
            "user.username, user.id userId from answer left join user on answer.answerer=user.id " +
            "where answer.question_id=#{questionId} ${ew.customSqlSegment}")
    List<AnswerVO> listAnswers(Long questionId, @Param("ew") QueryWrapper<AnswerVO> wrapper);

    @Select("select answer.*, question.id question_id, question.title, " +
            "user.username from answer left join user on answer.answerer=user.id left join question on answer.question_id=question.id " +
            "where answer.answerer=#{userId} order by answer.create_time desc")
    IPage<AnswerVO> listAnswersByUserId(IPage<AnswerVO> page, Integer userId);

    @Select("SELECT question.title, question.is_top, question.is_essential, question.content questionContent, question.answer_count, question.id question_id, question.create_time questionCreateTime, question.update_time questionUpdateTime, " +
            "answer.*, user.username FROM question left join user on question.creator=user.id left join answer on answer.question_id=question.id " +
            "WHERE question.title like CONCAT('%', #{keyword}) or question.title like CONCAT(#{keyword}, '%')" +
            "or question.title like CONCAT('%', #{keyword}, '%') or question.title=#{keyword} or answer.content like CONCAT('%', #{keyword}, '%') ${ew.customSqlSegment}")
    IPage<AnswerVO> listAnswersByKeyword(IPage<AnswerVO> page, @Param("ew") Wrapper<AnswerVO> wrapper, String keyword);
}
